package doc2db.table;

import java.util.ArrayList;
import java.util.List;

public class DocTableHead {

    private List<DocTableCell> headList = new ArrayList<DocTableCell>() {

        @Override
        public boolean add(DocTableCell e) {
            for (int i = 0; i < this.size(); i++) {
                final String s1 = this.get(i).getName();
                final String s2 = e.getName();
                if (s1.equals(s2)) {
                    return false;
                }
            }
            return super.add(e);
        }
    };
    private final String type = "varchar";

    public DocTableHead() {
    }

    public DocTableHead(String[] a) {
        for (int i = 0; i < a.length; i++) {
            headList.add(new DocTableCell(a[i].toLowerCase().trim(), type));
        }
    }

    public String[] get() {
        String[] a = new String[headList.size()];
        int k = 0;
        for (int i = 0; i < headList.size(); i++) {
            String s = "" + headList.get(i).getName();
            a[k++] = s;
        }
        return a;
    }

    public DocTableCell get(int where) {
        return headList.get(where);
    }

    public void add(String s) {
        headList.add(new DocTableCell(s.toLowerCase().trim(), type));
    }

    public void add(String s, int len) {
        headList.add(new DocTableCell(s.toLowerCase().trim(), type, len));
    }

    public void add(DocTableCell c) {
        headList.add(c);
    }

    public void remove(String s) {
        //headList.remove(s);
        for (int i = 0; i < get().length; i++) {
            String hn = get(i).getName();
            if (hn == null ? s == null : hn.equals(s)){
                headList.remove(i);
                return;
            }
        }
    }

    public void remove(int index) {
        headList.remove(index);
    }

    public void clear() {
        headList.clear();
    }

    public String getSQLString() {

        StringBuilder a = new StringBuilder();
        for (int i = 0; i < headList.size(); i++) {
            String m = "" + headList.get(i).getName();
            String t = "" + headList.get(i).getHead();
            a.append("  \"");
            a.append(m);
            a.append("\"");
            a.append(" ");
            a.append(t);
            a.append(",\n");
        }

        a.deleteCharAt(a.length() - 1);
        a.deleteCharAt(a.length() - 1);
        return a.toString();
    }

    public int getSize() {
        return headList.size();
    }

    public void setHeadType(int where, String what) {
        headList.get(where).setHead("" + what);
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder("\n");
        int i = 0;
        for (DocTableCell c : headList) {
            ret.append("    [").append(i++).append("]").append(c.toString()).append("\n");
        }
        return ret.toString();
    }
}
